﻿using System;
using NDBGen.Descriptors.Columns.Tokens;

namespace NDBGen.Plugins.Descriptors.Columns.Tokens
{
    [ColumnTokenScriptSnippetGenerator(typeof(ForeignKeyTokenDescriptor))]
    public sealed class SqlServerForeignKeyTokenCodeGenerator : SqlServerColumnTokenScriptGeneratorBase<ForeignKeyTokenDescriptor>, IOrderedMetadata
    {
        private const String SQL_FORMAT = "constraint [FK__{0}_{2}__{1}_{4}] foreign key ([{2}]) references [{3}].[{0}] ([{4}])";

        protected override void WriteSql(IIndentedTextWriter writer)
        {
            writer.Write(SQL_FORMAT, this.Descriptor.ForeignKeyTable, this.Descriptor.Table.Name, this.Descriptor.Column.Name, this.Descriptor.ForeignKeySchema, this.Descriptor.ForeignKeyColumn);            
        }

        Int32 IOrderedMetadata.GenerationIndex { get { return 5; } }
    }
}